const app = getApp(), pageSize = 15;
let page = 1;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    noData: false, //没有数据的时候
    showList: true,
    x: wx.getSystemInfoSync().windowWidth - 50,
    y: wx.getSystemInfoSync().windowHeight - 150,
    loadmore: false, //加载更多
    nomore: false, //没有更多
    list: [],//列表数据
    eqSn: "",//设备编号
    s_time: "", //搜索开始时间
    e_time: "", //搜索结束时间
  },

  onUnload: function () {
    page = 1;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { eq_sn } = options;
    this.setData({
      eqSn: eq_sn
    })
    this.getConsumeList();
  },

  //获取消费列表
  getConsumeList() {
    app.ajax({
      url: 'Manage/Equipment/sell_water',
      data: {
        page: page,
        row: pageSize,
        equipment_number: this.data.eqSn,
        start_time: this.data.s_time,
        end_time: this.data.e_time
      },
      success: res => {
        if (res.data.code == 200) {
          let source = res.data.data;
          let list = this.data.list;
          if (page == 1) {
            list = source;
          } else {
            list = list.concat(source);
          }
          let nomore = false, loadmore = false;
          if (source.length < pageSize) {
            nomore = true;
          } else {
            loadmore = true;
          }
          this.setData({
            nomore,
            loadmore,
            list
          })
        } else {
          if (page == 1) {
            this.setData({
              noData: true
            })
          } else {
            this.setData({
              loadmore: false,
              nomore: true
            })
          }
        }
      }
    })
  },


  onReady() {
    this.startTimePicker = this.selectComponent("#dateTime-start");
    this.endTimePicker = this.selectComponent("#dateTime-end");
  },

  //展示开始时间picker
  showStartPicker() {
    this.startTimePicker.show();
  },


  //确定开始时间
  changeStartTime(e) {
    this.setData({
      s_time: e.detail.result
    })
  },
  //确定结束时间
  changeEndTime(e) {
    this.setData({
      e_time: e.detail.result
    })
  },

  //展示结束时间picker
  showEndPicker() {
    this.endTimePicker.show();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(this.data.showList);
    if (this.data.showList) {
      page = 1;
      this.getConsumeList();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore == true) {
      page++;
      this.getConsumeList();

    }
  },

  showSearch() {
    this.setData({
      showList: false
    })
  },

  goBack() {
    this.setData({
      showList: true
    })
  },

  doSearch() {
    if (this.data.s_time == "") {
      app.showToast("请选择开始时间"); return;
    } else if (this.data.e_time == "") {
      app.showToast("请选择结束时间"); return;
    }
    page = 1;
    this.getConsumeList();
    this.setData({
      showList: true
    })
  }


})